package com.forezp.datastruct;

/**
 * 进制转换
 * Created by forezp on 2017/12/5.
 * <p>
 * 短除法
 * <p>
 * <p>
 * N      N div 8  N mod 8
 * 1348   168       4
 * 168    21        0
 * 21     2        5
 * 2      0        2
 * <p>
 * <p>
 * <p>
 * <p>
 * N   N div 16  N mod 16
 * 1348   84    4
 * 84     5     4
 * 5   0        5
 */
public class HexConversion {

    enum HexType {
        Binary(2), Octal(8), Hex(16);

        HexType(int code) {
            this.code = code;
        }

        int code;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }

    private final char[] chars = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public void HexCon(int num, HexType type) {

        ArrayStack<Integer> arrayStack = new ArrayStack<>(30);

        while (num!=0){
            arrayStack.push(num%type.getCode());
            num=num/type.getCode();
        }

        while (!arrayStack.isEmpty()){
            System.out.print(chars[arrayStack.pop()]);
        }

    }

    public static void main(String[] args) {
        HexConversion hexConversion=new HexConversion();
        hexConversion.HexCon(10,HexType.Hex);
    }


}
